package com.orange.controller.school;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.handler.AuthItemLevelMapService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.AuthItemLevelMap;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/itemLevel")
public class AuthItemLevelMapController extends BaseController{

	private static final Logger logger = Logger.getLogger(AuthItemLevelMapController.class);
	
	@Autowired
	private AuthItemLevelMapService authItemLevelMapService;
	
	/**
	 * 新增自评等级细则
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertitemLevel" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertitemLevel(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		try{
			
			String pid=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==pid){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			String subject=(String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);
			
			String itemId = map.get("itemId");//分项id
			String detailLevelId = map.get("detailLevelId");//细则等级Id
			String detailLevel = map.get("detailLevel");//细则等级
			if(detailLevelId==null || StringUtils.isBlank(detailLevelId))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"等级Id不能为空");
			if(detailLevel==null || StringUtils.isBlank(detailLevel))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"等级不能为空");
			String[] detailLevelIdList = detailLevelId.split(",");
			String[] detailLevelList = detailLevel.split(",");
			List<AuthItemLevelMap> list=new ArrayList<AuthItemLevelMap>();
			for(int i=0 ; i<detailLevelIdList.length;i++){
				AuthItemLevelMap ailm = new  AuthItemLevelMap();
				ailm.setEventId(detailLevelIdList[i]);
				ailm.setEventLevel(detailLevelList[i]);
				ailm.setItemId(itemId);
				ailm.setLoginType(subject);
				ailm.setSid(pid);
				list.add(ailm);
			}
			authItemLevelMapService.deleteItemlevelMap(itemId,pid, subject);
			authItemLevelMapService.insertItemlevelMap(list);
			return toResponse(null,ReturnCode.SUCCESS,"新增自评等级细则成功");
		}catch(Exception e){
			logger.error("新增自评等级细则失败",e);
			return toResponse("null",ReturnCode.FAIL,"新增自评等级细则失败");
		}
		
	}
	
}
